package com.deercrowing.takeaway.controller.admin;


import com.deercrowing.takeaway.common.exception.BusinessException;
import com.deercrowing.takeaway.common.model.Result;
import com.deercrowing.takeaway.vo.CategoriesPageVO;
import com.deercrowing.takeaway.vo.CategoriesVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import jakarta.validation.constraints.NotNull;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import com.deercrowing.takeaway.entity.CategoriesEntity;
import com.deercrowing.takeaway.service.CategoriesService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类表(Categories)表控制层
 *
 * @author guoyongxiang
 * @since 2024-07-10 15:10:04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("admin/categories")
public class CategoriesController {
    /**
     * 服务对象
     */
    @Autowired
    private CategoriesService categoriesService;

    /**
     * 分页查询所有数据
     *
     * @param categoriesPageVO 查询实体
     * @return 所有数据
     */
    @GetMapping("/list")
    public ResponseEntity selectAll(CategoriesPageVO categoriesPageVO) {
        return ResponseEntity.ok(Result.OK(this.categoriesService.searchByCondition(categoriesPageVO)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public ResponseEntity selectOne(@PathVariable @NotNull(message = "主键不能为空") Integer id) {
        if (ObjectUtils.isEmpty(this.categoriesService.getById(id)))
        {
            throw new NullPointerException();
        }
        return ResponseEntity.ok(Result.OK(this.categoriesService.searchById(id)));
    }

    /**
     * 新增数据
     *
     * @param categoriesVO 实体对象
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity insert(@RequestBody CategoriesVO categoriesVO) {
        if (ObjectUtils.isEmpty(categoriesVO)){
            throw new NullPointerException();
        }

        this.categoriesService.insertInfoToDB(categoriesVO);
        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 修改数据
     *
     * @param id 主键, categoriesVO 实体对象
     * @return 修改结果
     */
    @PutMapping("/{id}")
    public ResponseEntity update(@PathVariable @NotNull(message = "主键不能为空") Integer id, @RequestBody CategoriesVO categoriesVO) {

        this.categoriesService.updateInfoToDB(id,categoriesVO);
        return ResponseEntity.ok(Result.OK());
    }
    
    /**
     * 通过主键删除单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @DeleteMapping("/{id}")
    public ResponseEntity delete(@PathVariable @NotNull(message = "主键不能为空") Integer id) {
        if (ObjectUtils.isEmpty(this.categoriesService.getById(id)))
        {
            throw new NullPointerException();
        }
        categoriesService.deleteConnectRelates(id);

        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public ResponseEntity delete(@RequestParam("idList") List<Integer> idList) {
        if (idList.size() < 0){
            throw new BusinessException("请勾选要删除的分类");
        }

        categoriesService.deleteConnectRelates(idList);
        return ResponseEntity.ok(Result.OK(this.categoriesService.removeByIds(idList)));
    }

    /**
     * 修改分类状态
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/status")
    public ResponseEntity statusUpdate(@RequestParam @NotNull(message = "主键不能为空") Integer id,
                                       @RequestParam @NotNull(message = "状态不能为空") Short status){


        this.categoriesService.categoryStatusUpdate(status,id);
        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 根据类型查询所属分类
     *
     * @param type
     * @return
     */
    @GetMapping("/type")
    public ResponseEntity selectByType(@RequestParam @NotNull(message = "分类类型不能为空") Short type){
        return ResponseEntity.ok(Result.OK(this.categoriesService.searchByType(type)));
    }
}

